package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MenuMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public Result queryByUsername(String username) {
        Integer roleId = menuMapper.findRoleIdByUsername(username);
        List<Integer> menuIds = menuMapper.findMenuIdsByRoleId(roleId);
        List<Menu> menus = menuMapper.findMenusByMenuIds(menuIds);
        for (Menu menu : menus) {
            Integer parentId = menu.getId();
            menu.setChildren(menuMapper.findMenusByParentId(parentId));
        }
        return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS,menus);
    }


    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        List<Menu> Menus = menuMapper.findPage(queryPageBean.getQueryString());

        return PageResult.builder().rows(page.getResult()).total(page.getTotal()).build();

    }
}
